package czh.cocoa.ui.impl;

import static czh.cocoa.ui.UIKit.UIGraphicsBeginImageContext;
import static czh.cocoa.ui.UIKit.UIGraphicsEndImageContext;
import static czh.cocoa.ui.UIKit.UIGraphicsGetImageFromCurrentImageContext;
import czh.cocoa.animation.CALayer;
import czh.cocoa.graphics.CGImage;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIView;

public class UIImageView extends UIView {

	private UIImage mImage;
	
	private UIImage mStretchImage;
	
	public UIImageView() {
		
	}

	public UIImageView(CGRect frame) {
		super(frame);
	}
	
	protected void init() {
		super.init();
		setUserInteractionEnabled(false);
		setClipsToBounds(true);
	}
	
	public UIImageView(UIImage image) {
		this(image, true);
	}
	
	public UIImageView(UIImage image, boolean autoresize) {
		if (image != null) {
			CGSize size = image.size();
			if (autoresize) {
				size = DIP(size);
			}
			setFrame(new CGRect(0, 0, size.width, size.height)); 	
			setImage(image);
		}
		setUserInteractionEnabled(false);
	}
	
	public void setImage(UIImage image) {
		mImage = image;
		resizeImageContents();
		layer().setContents(mStretchImage != null ? mStretchImage.CGImage() : null);
	}
	
	public UIImage image() {
		return mImage;
	}
	
	public boolean displayLayer(CALayer layer) {
		return true;
	}
	
	public void setNeedsDisplay() {
		
	}
	
	public void setBackgroundColor(UIColor color) {
		layer().setBackgroundColor(color.CGColor());
	}
	
	public UIColor backgroundColor() {
		return new UIColor(layer().backgroundColor());
	}
	
	public void setContentStretch(CGRect stretch) {
		super.setContentStretch(stretch); 
		boolean resized = resizeImageContents();
		if (resized) {
			layer().setContents(mStretchImage.CGImage());
		}
	}
	
	private boolean resizeImageContents() {
		if (contentMode() == UIViewContentModeScaleToFill) {
			CGRect stretch = contentStretch();
			if (stretch.size.width < 1 || stretch.size.height < 1) {
				if (mImage != null) {
					CGSize size = mImage.size();
					CGRect frame = frame();
					if (size.width < frame.size.width || size.height < frame.size.height) { 
						int left = (int)(size.width * stretch.origin.x); 
						int top = (int)(size.height * stretch.origin.y);
						int right =  (int)(size.width * (1 - stretch.origin.x - stretch.size.width));
						int bottom = (int)(size.height * (1 - stretch.origin.y - stretch.size.height));

						UIImage image = mImage.resizableImageWithCapInsets(UIEdgeInsetsMake(top, left, bottom, right));
						 
						//NSLog("image resize: %s %d %d %d %d", size, left, top, right, bottom);
						
						CGRect bounds = bounds();
						float scale = 1;
						if (size.width > frame.size.width) { 
							scale = size.width / frame.size.width;
						} else if (size.height > frame.size.height) {
							scale = size.height / frame.size.height;
						}
						if (scale != 1) {
							bounds.size.width *= scale;
							bounds.size.height *= scale;
						}
						
						UIGraphicsBeginImageContext(bounds.size); 
						image.drawInRect(bounds);
						mStretchImage = UIGraphicsGetImageFromCurrentImageContext();
						if (scale != 1) {
							CGImage img = mStretchImage.CGImage();
							mStretchImage = new UIImage(img, scale);
						}
						UIGraphicsEndImageContext();
						
						return true;
					}
				}
			}
		}
		
		mStretchImage = mImage;
		
		return false;
	}
	
	public void layerSizeDidChanged(CALayer layer, CGRect oldFrame) {
		super.layerSizeDidChanged(layer, oldFrame);
		boolean resized = resizeImageContents();
		if (resized) {
			layer().setContents(mStretchImage.CGImage());
		}
	}

}
